<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订阅管理</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/layer/layer.js"></script>
</head>
<body>

<div id="app">
    <nav class="navbar navbar-inverse" style="border-radius: 0">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">小黑资源系统</a>
            </div>

            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li><a href="/index.html">首页</a></li>
                    <li v-for="d in userResources" v-if="d.pid==0">
                        <!--  “v-...” 指的是vue里面的东西，主要用于ajax的内容显示  -->
                        <a v-bind:href="d.url">{{d.name}}</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right hidden-sm">
                    <li><a>欢迎您：{{userName}}</a></li>
                    <!--      "{{}}" 里面放       -->
                    <li><a @click="logout()" href="#">退出</a></li>

                </ul>
            </div>
        </div>
    </nav>

    <!--  页面主体  -->
    <div class="container">
        <h2>订阅管理</h2>
        <a href="#" type="button" class="btn btn-success" @click="groupAdd()">添加</a>
        <div class="row">
            <div class="col-sm-12">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>订阅名</th>
                        <th>备注</th>
                        <th>添加时间</th>
                        <th>修改时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="g in pageObj.list">
                        <td>{{g.id}}</td>
                        <td>{{g.name}}</td>
                        <td>{{g.remark}}</td>
                        <td>{{g.createTime}}</td>
                        <td>{{g.updateTime}}</td>
                        <td>
<!--                            <a href="#" @click="editResource(u.id)" class="btn btn-warning">授权</a>-->
                            <a href="#" @click="updateId(g.id)" class="btn btn-primary">修改</a>
                            <a class="btn btn-danger" @click="deleteId(g.id)">删除</a>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div align="center">
        <nav aria-label="Page navigation">
            <ul class="pagination" style="text-align: center">
                <li v-if="pageObj.page>1">
                    <a href="#" aria-label="Previous" @click="subPage()"><span
                            aria-hidden="true">上一页</span>
                    </a>
                </li>
                <li v-for="d in pageObj.pageCount" v-bind:class="d==pageObj.page?'active':''" @click="goPage(d)">
                    <a href="#">{{d}}</a>
                </li>
                <li v-if="pageObj.page<pageObj.pageCount">
                    <a href="#" aria-label="Next" @click="sumPage()">
                        <span aria-hidden="true">下一页</span>
                    </a>
                </li>
                <li><a>共 {{pageObj.pageCount}} 页</a></li>
            </ul>
        </nav>
    </div>
</div>

<script>
    new Vue({
        el: "#app",
        data: {
            userResources: [],
            userName: '',
            groupList: {},
            page: 1,
            size: 8,
            pageObj: {}
        },
        created() {   //创建
            this.getUserInfo();
            this.loadData();
        },
        methods: {
            //对象显示
            getUserInfo() {
                var vm = this;
                $.post("/userInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.userName = response.data.name
                        vm.userResources = response.data.role.resourceList
                    } else {
                        location.href = "/login.html"
                    }
                })
            },
            //显示对象
            loadData() {
                var vm = this;
                $.post("/group/list", {page: vm.page, size: vm.size}, function (response) {
                    if (response.code == 200) {
                        vm.pageObj = response.data;
                    }
                })
            },
            goPage(page) {
                this.page = page;
                this.loadData();
            },
            //上一页
            subPage() {
                this.page--;
                this.loadData();
            },
            //下一页
            sumPage() {
                this.page++;
                this.loadData();
            },
            //添加
            groupAdd(){
                var vm = this;
                layer.open({
                    type: 2,
                    title: '订阅添加',
                    shadeClose: false,  //阴影
                    shade: 0.5,  //透明度
                    area: ['600px', '100%'],
                    content: 'add.html', //iframe的url
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            updateId(id){
                var vm = this;
                layer.open({
                    type: 2,
                    title: '角色修改',
                    shadeClose: false,  //阴影
                    shade: 0.5,  //透明度
                    area: ['600px', '100%'],
                    content: 'update.html?id=' + id,//iframe的url
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            //删除
            deleteId(id){
                var vm = this;
                layer.confirm('您确定要删除该角色？', {
                    btn: ['删除', '取消'] //按钮
                }, function () {
                    $.post("/group/deleteId", {id: id}, function (response) {
                        if (response.code == 200) {
                            layer.msg('删除成功！', {
                                time: 1000,
                                end: function () {
                                    vm.loadData();
                                }
                            })
                        }
                    })
                }, function () {
                });
            },
            //退出
            logout() {
                $.post("/logout", {}, function (response) {
                    layer.msg('退出成功！', {
                        time: 500,
                        end: function () {
                            location.href = "/login.html"
                        }
                    })
                })
            }
        }
    })
</script>
</body>
</html>